home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5688 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  94 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!news
  3. From: rclark@iquest.net (Robert B. Clark)
  4. Subject: Re: Ability to locate spaces?
  5. X-Nntp-Posting-Host: ind-004-236-169.iquest.net
  6. Message-ID: <3129dc3a.706665@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Internet, Inc.
  9. X-Newsreader: Forte Agent .99d/16.182
  10. References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
  11. Date: Tue, 20 Feb 1996 15:45:03 GMT
  12.  
  13. On Thu, 15 Feb 1996 22:30:23 -0500, John Young Oh <jyo@acpub.duke.edu>
  14. wrote:
  15.  
  16. >I am trying to write a program that reads an input file and locates where
  17. >the spaces are located. For example, if a line from the input file read:
  18. >555555.55    5555.55   5555.55
  19. >
  20. >I would like the program to be able to recognize that there are spaces
  21. >between the numbers and keep track of them. What I did was to use
  22. >fgets and read in the line into a string. I then used a for loop
  23. >and checked each array element of the character string and used an
  24. >if statement to see if an array element was a space.
  25. >Here are the lines of code:
  26. >
  27. >FILE *input;
  28. >char *string, buf[200];
  29. >int i, count;
  30. >input = fopen ("data", "r");
  31. >(etc etc etc)
  32. >string = fgets (buf, 199, input);
  33. >for (i = 0; i < 40; ++i)
  34. >{
  35. >    if (buf[i] == ' ')
  36. >    {
  37. >        ++count;
  38. >    }
  39. >}
  40. >
  41. >What is wrong with this code? It does not seem to work.
  42.  
  43. Pretty vague.  What sort of errors are you getting?
  44.  
  45. Is 'count' not correct?  If not, do you initialize 'count' to zero in
  46. the "etc etc etc" code?
  47.  
  48. Is the data file "data" correct?  Does it exist?
  49.  
  50. Are you actually getting anything from the fgets() call?  Your code
  51. doesn't check for read errors, so if the file were empty you'd never
  52. know it.
  53.  
  54. I don't know if you want to keep track of the offsets of the spaces (per
  55. your text in the first paragraph) or simply count the number of spaces
  56. (per your code).  For the latter, why not simply use strchr()
  57. recursively:
  58.  
  59. /* count.c - Recursively count spaces in string.  Turbo C v3.0
  60.    Written 19 Feb 1996 by Robert B. Clark <rclark@iquest.net>
  61.    Donated to the public domain. */
  62.  
  63. #include <stdio.h>
  64. #include <string.h>     /* For strchr() function */
  65.  
  66. int count_spaces(const char *s)
  67. {
  68.    static int count;
  69.    char *p;
  70.  
  71.    if ((p=strchr(s,' '))!=NULL) {   /* Find next space character */
  72.       count++;                      /* Increment counter */
  73.       count_spaces(p+1);            /* Go back for more */
  74.    }
  75.    return count;
  76. }
  77.  
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.    char str[50]="This string contains four spaces.";
  82.    int x;
  83.  
  84.    if (argc>1) strcpy(str,argv[1]);
  85.    x=count_spaces(str);
  86.    printf("\n\"%s\" has %d ASCII 0x20 chars.",str,x);
  87.    return x;
  88. }
  89.  
  90. --
  91. Robert B. Clark <rclark@iquest.net>
  92. "Be wary of strong spirits.  It can make you shoot at tax collectors...
  93. and miss." --RAH
  94.